#include "execute_task.h"

TaskHandle_t executeHandler;

void showContrl();
void alarmContrl();
void airContrl();
void curtainContrl();

extern TargetGatherData targetData;
THData thData;
// 执行模块初始化
void init_execute()
{
    if (thData.state == 0)
    {
        thData.state=15;
        thData.tempMax=200;
        thData.tempMin=-20;
        thData.humiMax=200;
        thData.humiMin=20;
        thData.sunshine=100;
        thData.smoke=200;

    }
    //对可能要用到的外设做初始化
    ledInit();
    BEEP_Init();
    OLED_Init();

}
// 执行模块的任务
void execute_task(void *param)
{

    while (1)
    {
        //判断用户要做的操作
        if(thData.state&0x01){
            showContrl();
        }
        if(thData.state&0x02){
            alarmContrl();
        }
        if(thData.state&0x04){
            airContrl();
        }
        if(thData.state&0x08){ 
            curtainContrl();
        }

        vTaskDelay(10);     
    }
}

void createExecuteTask()
{
    xTaskCreate(
        execute_task,
        "execute_task",
        128,
        NULL,
        2,
        &executeHandler);
}

void showContrl(){
    OLED_Clear();
	OLED_ShowString(0,0,  "Temp:  C",12);
	OLED_ShowString(0,15, "Humi:  %",12);
    OLED_ShowNum(5*6,0,targetData.temp,2,12);
	OLED_ShowNum(5*6,15,targetData.humi,2,12);
    OLED_Refresh_Gram();
}
void alarmContrl(){
    u8 beepflag=0;
    if(targetData.temp < thData.tempMin || targetData.temp > thData.tempMax){
        //点亮led灯
        LED1=0;
        beepflag=1;
    }else
    {
        LED1=1;
    }
    if(targetData.humi < thData.humiMin || targetData.humi > thData.humiMax){
        //点亮led灯
        LED2=0;
        beepflag=1;
    }else
    {
        LED2=1;
    }

    if(targetData.smoke > thData.smoke){
        LED3=0;
        beepflag=1;
    }else
    {
        LED3=1;
    }
    
    if(targetData.sunshine > thData.sunshine){
        LED4=0;
        beepflag=1;
    }else
    {
        LED4=1;
    }
    
    if(!targetData.raidrop){
        LED5=0;
        beepflag=1;
    }else
    {
        LED5=1;
    }
    


    if(!beepflag){
        BEEP=1;//不响
    }else
    {
        while (1)
        {
            BEEP=!BEEP;
            vTaskDelay(20);
        }
        
    }
    
}

 //空调  控制  发送红外信息
void airContrl(){
   

}
//控制窗帘   就是控制电机
void curtainContrl(){

}